/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame;

import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bounding.BoundingVolume;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.PhysicsTickListener;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.collision.Collidable;
import com.jme3.collision.CollisionResults;
import com.jme3.collision.UnsupportedCollisionException;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.SceneGraphVisitor;
import com.jme3.scene.Spatial;
import java.util.Queue;

/**
 *
 * @author Ruben
 */
public class ShipFactory extends RigidBodyControl {
    private float shipRotation, shipSpeed;
    private Quaternion dirView;
    private Vector3f dirMove;
    private Spatial shipModel;

    
    public ShipFactory(CollisionShape shape, float mass, BulletAppState aps){
        super(shape, mass);
        
        shipRotation = 1f;
        shipRotation = 1f;
        
        aps.getPhysicsSpace().addTickListener(physics);
    }
    
    public void setMoveSpeed(float value) {
        shipSpeed = value;
    }
    
    public void setRotationSpeed(float value) {
        shipRotation = value;
    }
    
    public void setFlyDirection(Vector3f direction) {
        dirMove = direction;
    }
    
    public void setViewDirection(Quaternion viewDirection){
        dirView = viewDirection;
    }
    
    PhysicsTickListener physics = new PhysicsTickListener(){
            public void prePhysicsTick(PhysicsSpace space, float f) {
                //moving the ship
                if(dirMove != null) {
                    applyCentralForce(dirMove.mult(shipSpeed));
                    dirMove = null;
                }
                //ship rotation
                if(dirView != null) {
                    Vector3f dirSpatial = getPhysicsRotation().mult(Vector3f.UNIT_Z);
                    Vector3f dirCam = dirView.mult(Vector3f.UNIT_Z);
                    Vector3f cross = dirSpatial.cross(dirCam);

                    Vector3f dirSpatial1 = getPhysicsRotation().mult(Vector3f.UNIT_Y);
                    Vector3f dirCam1 = dirView.mult(Vector3f.UNIT_Y);
                    Vector3f cross1 = dirSpatial1.cross(dirCam1);

                    Vector3f dirSpatial2 = getPhysicsRotation().mult(Vector3f.UNIT_X);
                    Vector3f dirCam2 = dirView.mult(Vector3f.UNIT_X);
                    Vector3f cross2 = dirSpatial2.cross(dirCam2);                    
                    
                    applyTorque(cross.add(cross1).add(cross2).normalizeLocal().mult(shipRotation));
                    
                    dirView = null;
                }
            }
            
            public void physicsTick(PhysicsSpace space, float f) {
                
            }

    };
    
}
